package com.aplink.generic.dialog;

import java.io.Serializable;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.Html;
import android.text.TextUtils;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.type.DialogStatus;
import com.aplink.generic.dictionary.GenericDictionary;

public class ApLinkAlertDialog implements DialogInterface.OnClickListener,
        Serializable {
    private static final long serialVersionUID = -1655995347950817026L;
    private boolean mCancelable;
    private boolean mCanceledOnTouchOutside;
    private DialogInfo mDialogInfo;
    private final GenericDictionary<String, DialogInfo> mDialogInfos;
    private DialogStatus mDialogStatus;
    private GenericFragment mGenericFragment;

    public ApLinkAlertDialog() {
        this.mDialogInfos = new GenericDictionary<String, DialogInfo>();
    }

    public AlertDialog getAlertDialog(final DialogStatus key) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(
                this.mGenericFragment.getActivity());
        this.mDialogInfo = this.mDialogInfos.get(key.getValue());
        this.mDialogStatus = key;
        if (mDialogInfo.getText() != null) {
            String message = null;
            if (mDialogInfo.isFormatHTML()) {
                message = Html.fromHtml(mDialogInfo.getText()).toString();
            } else {
                message = mDialogInfo.getText();
            }
            builder.setMessage(message);
        }
        if (mDialogInfo.getTitle() != null) {
            String title = null;
            if (mDialogInfo.isFormatHTML()) {
                title = Html.fromHtml(mDialogInfo.getTitle()).toString();
            } else {
                title = mDialogInfo.getTitle();
            }
            builder.setTitle(title);
        }
        if (mDialogInfo.getNegativeButton() != null) {
            String negativeButton = null;
            if (mDialogInfo.isFormatHTML()) {
                negativeButton = Html.fromHtml(
                        mDialogInfo.getNegativeButton().getText()).toString();
            } else {
                negativeButton = mDialogInfo.getNegativeButton().getText();
            }
            builder.setNegativeButton(negativeButton, this);
        }
        if (mDialogInfo.getPositiveButton() != null) {
            String positiveButton = null;
            if (mDialogInfo.isFormatHTML()) {
                positiveButton = Html.fromHtml(
                        mDialogInfo.getPositiveButton().getText()).toString();
            } else {
                positiveButton = mDialogInfo.getPositiveButton().getText();
            }
            builder.setPositiveButton(positiveButton, this);
        }
        if (mDialogInfo.getNeutralButton() != null) {
            String neutralButton = null;
            if (mDialogInfo.isFormatHTML()) {
                neutralButton = Html.fromHtml(
                        mDialogInfo.getNeutralButton().getText()).toString();
            } else {
                neutralButton = mDialogInfo.getNeutralButton().getText();
            }
            builder.setNeutralButton(neutralButton, this);
        }
        return builder.create();
    }

    public AlertDialog getAlertDialog(final DialogStatus key,
            String customMessage) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(
                this.mGenericFragment.getActivity());
        this.mDialogInfo = this.mDialogInfos.get(key.getValue());
        this.mDialogStatus = key;
        if (mDialogInfo.getText() != null) {
            String message = null;
            if (TextUtils.isEmpty(customMessage)) {
                customMessage = mDialogInfo.getText();
            }
            if (mDialogInfo.isFormatHTML()) {
                message = Html.fromHtml(customMessage).toString();
            } else {
                message = customMessage;
            }
            builder.setMessage(message);
        }
        if (mDialogInfo.getTitle() != null) {
            String title = null;
            if (mDialogInfo.isFormatHTML()) {
                title = Html.fromHtml(mDialogInfo.getTitle()).toString();
            } else {
                title = mDialogInfo.getTitle();
            }
            builder.setTitle(title);
        }
        if (mDialogInfo.getNegativeButton() != null) {
            String negativeButton = null;
            if (mDialogInfo.isFormatHTML()) {
                negativeButton = Html.fromHtml(
                        mDialogInfo.getNegativeButton().getText()).toString();
            } else {
                negativeButton = mDialogInfo.getNegativeButton().getText();
            }
            builder.setNegativeButton(negativeButton, this);
        }
        if (mDialogInfo.getPositiveButton() != null) {
            String positiveButton = null;
            if (mDialogInfo.isFormatHTML()) {
                positiveButton = Html.fromHtml(
                        mDialogInfo.getPositiveButton().getText()).toString();
            } else {
                positiveButton = mDialogInfo.getPositiveButton().getText();
            }
            builder.setPositiveButton(positiveButton, this);
        }
        if (mDialogInfo.getNeutralButton() != null) {
            String neutralButton = null;
            if (mDialogInfo.isFormatHTML()) {
                neutralButton = Html.fromHtml(
                        mDialogInfo.getNeutralButton().getText()).toString();
            } else {
                neutralButton = mDialogInfo.getNeutralButton().getText();
            }
            builder.setNeutralButton(neutralButton, this);
        }
        return builder.create();
    }

    public GenericFragment getGenericFragment() {
        return this.mGenericFragment;
    }

    public boolean has(final DialogStatus alertDialogStatus) {
        if (alertDialogStatus == null) {
            return false;
        }
        return this.mDialogInfos.containsKey(alertDialogStatus.getValue());
    }

    public void init(final GenericFragment genericFragment) {
        this.mGenericFragment = genericFragment;

    }

    public boolean isCancelable() {
        return this.mCancelable;
    }

    public boolean isCanceledOnTouchOutside() {
        return this.mCanceledOnTouchOutside;
    }

    @Override
    public void onClick(final DialogInterface dialog, final int which) {
        if (this.mDialogInfo != null) {
            DialogButton dialogButton = null;
            if (which == DialogInterface.BUTTON_POSITIVE) {
                dialogButton = this.mDialogInfo.getPositiveButton();
            } else if (which == DialogInterface.BUTTON_NEUTRAL) {
                dialogButton = this.mDialogInfo.getNegativeButton();
            } else {
                dialogButton = this.mDialogInfo.getNeutralButton();
            }

            if (dialogButton != null) {
                if (dialogButton.getActionType() != null) {
                    if (mDialogStatus.equals(DialogStatus.CONFIRM)) {
                        this.mGenericFragment.submit();
                    } else {
                        this.mGenericFragment.getEventHandler().sendAction(
                                dialogButton.getActionType());
                    }
                }
                if (dialogButton.isLinkToNextPage()) {
                    this.mGenericFragment.nextPage();
                }
                if (dialogButton.isFinishActivity()) {
                    this.mGenericFragment.finishActivity();
                }

            }
        }
    }

    public void put(final String key, final DialogInfo dialogInfo) {
        this.mDialogInfos.put(key, dialogInfo);
    }

    public void setCancelable(final boolean cancelable) {
        this.mCancelable = cancelable;
    }

    public void setCanceledOnTouchOutside(final boolean canceledOnTouchOutside) {
        this.mCanceledOnTouchOutside = canceledOnTouchOutside;
    }

    public void show(final String key) {
    }
}
